package com.base.cn.platform.os.common.utils.http;

import com.base.cn.platform.os.common.utils.BaseUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.google.common.collect.Lists;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Http请求工具类
 */
public class HttpUtil extends BaseUtil{

    /**
     * GET请求
     * @param url URL地址
     * @return String 字符
     */
    public static String doGet(String url)throws IOException{
        String result = null;
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        CloseableHttpResponse response = client.execute(get);
        HttpEntity entity = response.getEntity();
        if(entity != null) result = EntityUtils.toString(entity, "utf-8");
        HttpClientUtils.closeQuietly(response);
        HttpClientUtils.closeQuietly(client);
        return result;

    }

    /**
     * 执行一个HTTP POST请求，返回请求响应的HTML
     * 
     * @param url 请求的URL地址
     * @param params 请求的查询参数,可以为null
     * @return 返回请求响应的HTML
     */
    public static String doPost(String url, Map<String, String> params)throws IOException {
        String result = null;
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        if (ObjectUtils.isNotEmpty(params)) {
            List<NameValuePair> paramsList = Lists.newArrayList();
            for (Map.Entry<String, String> entry : params.entrySet()) {
                paramsList.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
            HttpEntity httpEntity =new UrlEncodedFormEntity(paramsList, "utf-8");
            post.setEntity(httpEntity);
        }
        CloseableHttpResponse response = client.execute(post);
        HttpEntity entity = response.getEntity();
        if(entity != null) result =  EntityUtils.toString(entity, "utf-8");
        HttpClientUtils.closeQuietly(response);
        HttpClientUtils.closeQuietly(client);
        return result;
    }

    /**
     * 发送json格式的数据
     * @param url
     * @param params
     * @return
     * @throws IOException
     */
    public  static String doPostJson(String url, Map<String, String> params)throws IOException {
        String result = null;
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
            StringEntity s = new StringEntity(gson.toJson(params));
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");//发送json数据需要设置contentType
            post.setEntity(s);
            HttpResponse res = client.execute(post);
            HttpEntity entity = res.getEntity();
            if(entity != null) result =  EntityUtils.toString(entity, "utf-8");
            HttpClientUtils.closeQuietly(res);
            HttpClientUtils.closeQuietly(client);
         return result;
    }
}
